package Petgoods;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/*
卖家控制器，可以上架商品数量（对商品表product进行修改，增加对应商品数量）
 */
public class Sellcontrols {
    public void Shelf() {
        Scanner sc = new Scanner(System.in);
        while (true) {
            Sellermeau seller = new Sellermeau();
            seller.meau();
            int choice = sc.nextInt();
            sc.nextLine(); // 消耗掉换行符

            switch (choice) {
                case 1:
                    System.out.println("请输入想要增加商品名字（输入 '退出' 可结束操作）：");
                    String name = sc.nextLine();
                    if ("退出".equals(name)) {
                        continue;
                    }
                    System.out.println("增加的数量");
                    int num = sc.nextInt();
                    sc.nextLine(); // 消耗掉换行符
                    System.out.println("请输入商品价格：");
                    int price = sc.nextInt();
                    sc.nextLine(); // 消耗掉换行符
                    System.out.println("请输入卖家姓名：");
                    String sellerName = sc.nextLine();

                    Connection conn = null;
                    try {
                        conn = util.getConn();
                        // 检查商品是否已经存在
                        String checkSql = "SELECT id FROM product WHERE name = ?";
                        PreparedStatement checkPs = conn.prepareStatement(checkSql);
                        checkPs.setString(1, name);
                        ResultSet rs = checkPs.executeQuery();

                        if (rs.next()) {
                            // 商品已存在，增加数量
                            String updateSql = "UPDATE product SET number = number + ?, price = ?, seller_name = ? WHERE name = ?";
                            PreparedStatement updatePs = conn.prepareStatement(updateSql);
                            updatePs.setInt(1, num);
                            updatePs.setInt(2, price);
                            updatePs.setString(3, sellerName);
                            updatePs.setString(4, name);
                            int rows = updatePs.executeUpdate();
                            if (rows > 0) {
                                System.out.println("上架成功");
                            }
                        } else {
                            // 商品不存在，插入新记录
                            String insertSql = "INSERT INTO product (name, number, price, seller_name) VALUES (?,?,?,?)";
                            PreparedStatement insertPs = conn.prepareStatement(insertSql);
                            insertPs.setString(1, name);
                            insertPs.setInt(2, num);
                            insertPs.setInt(3, price);
                            insertPs.setString(4, sellerName);
                            int rows = insertPs.executeUpdate();
                            if (rows > 0) {
                                System.out.println("新商品上架成功");
                            }
                        }
                        util.closeResource(rs, conn, null);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                case 2:
                    return;
                default:
                    System.out.println("无效输入，请重新选择");
            }
        }
    }
    private JdbcUtil util = new JdbcUtil();
}